﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC3DEMO.Models;
using MVC3DEMO.Models.BS;

namespace MVC3DEMO.Controllers
{
    public class PostController : Controller
    {
        //
        // GET: /Post/
        private SQLHeperDataContext sqlheper;
        private BS_Post bs_post;
        public PostController()
        {
            sqlheper = new SQLHeperDataContext();
            bs_post = new BS_Post();
        }
        public ActionResult Index()
        {
            var listPost = bs_post.getListPost();
            return View(listPost);
        }

        //
        // GET: /Post/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Post/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Post/Create

        [HttpPost, ValidateInput(false)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                var post = new Post();
                post.Title = collection["Title"];
                post.Description = collection["Description"];
                post.Body = collection["Body"];
                post.DateCreate = DateTime.Parse(DateTime.Now.ToString());
                post.Priority = int.Parse(collection["Priority"].Trim());
                var show = collection["Show"];
                if (show == "false")
                {
                    post.Show = false;
                }
                else
                {
                    post.Show = true;
                }
                sqlheper.asp_InsertPost(post.Title, post.Description, post.Body, post.DateCreate, post.Priority, post.Show);
                return RedirectToAction("Create");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Post/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Post/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Post/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Post/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
